package com.example.ui.contact.db;

import java.util.Collections;
import java.util.LinkedList;

import com.example.ui.contact.bean.BaseItem;
import com.example.ui.contact.bean.ChatMsgItem;
import com.example.ui.contact.bean.MeItem;
import com.example.ui.contact.chat.L;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ChatMsgDB {
	public static final String MSG_DBNAME = "message.db";
	private static final String ChatMsg_TABLE_NAME = "chatMsg";
	private SQLiteDatabase db;
	@SuppressWarnings("static-access")
	public ChatMsgDB(Context context){
		db = context.openOrCreateDatabase(MSG_DBNAME,Context.MODE_PRIVATE,null);
		//		db.execSQL("CREATE table IF NOT EXISTS "
		//				+ ChatMsg_TABLE_NAME
		//				+ " (MeMsg_TABLE_NAME INTEGER PRIMARY KEY AUTOINCREMENT,content TEXT, time TEXT," +
		//				"name TEXT,userid TEXT,pic TEXT,isCome TEXT)");


		
	}

	public void saveChatMsg(String id, ChatMsgItem item){
		db.execSQL("CREATE table IF NOT EXISTS _"
				+ id
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,content TEXT, time TEXT," +
				"name TEXT,userid TEXT,pic TEXT,isCome TEXT)");
		
		String isCome = "1";
		if(item.getComMeg()==true){
			isCome = "0";
		}
		//打印id数据
		L.Debug("saveChatMsg id", id);
		
		db.execSQL(
				"insert into _"
						+ id
						+ " (content,time,name,userid,pic,isCome) "
						+"values(?,?,?,?,?,?)",
				new Object[] { item.getContent(), item.getTime(),item.getName(),
								item.getUserid(),item.getPic(), isCome});

	}

	public LinkedList<ChatMsgItem> getChatMsgList(String id, int pager) {
		int num = 10 * (pager + 1);
		LinkedList<ChatMsgItem> list = new LinkedList<ChatMsgItem>();
		db.execSQL("CREATE table IF NOT EXISTS _"
				+ id
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,userid TEXT,name TEXT, " +
				"time TEXT,content TEXT,pic TEXT,isCome TEXT)");		
		Cursor c = db.rawQuery("SELECT * from _" + id
				+ " ORDER BY _id DESC LIMIT " + num, null);
		while (c.moveToNext()) {


			String name = c.getString(c.getColumnIndex("name"));
			String time = c.getString(c.getColumnIndex("time"));
			String content = c.getString(c.getColumnIndex("content"));
			String userid = c.getString(c.getColumnIndex("userid"));
			String pic = c.getString(c.getColumnIndex("pic"));
			String isCome = c.getString(c.getColumnIndex("isCome"));

			ChatMsgItem item = new ChatMsgItem();
			item.setContent(content);
			item.setTime(time);
			item.setName(name);
			item.setUserid(userid);
			item.setPic(pic);			
			if (isCome.equals("0")) {
				item.setComMeg(true);
			}else{
				item.setComMeg(false);
			}
			list.add(item);
		}
		Collections.reverse(list);// 前后反转一下消息记录
		return list;
	}
	
	public void close() {
		if (db != null)
			db.close();
	}
	
	
	private boolean isExist(String userId) {
		Cursor c = db.rawQuery("SELECT * FROM " + MSG_DBNAME
				+ " WHERE userId = ?", new String[] { userId });
		return c.moveToFirst();
	}	
	
}
